/*
Copyright (c) [2019年5月1日] [吴超]
[MBT_studio] is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:
		 http://license.coscl.org.cn/MulanPSL2
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
*/
#include "stdafx.h"
#include "Vk缓存.h"

#include "S_键值序列容器.h"
#include "core/引擎配置.h"

//static S_键值序列容器<uint32, S_VkUniform缓存*> m_场景容器;
#include <mutex>




VkBufferUsageFlags f_getAPI缓存类型(E_板载缓存类型 memType) {
	uint32 mt = 0;

	switch (memType) {
	case e_VBO:		mt = VK_BUFFER_USAGE_VERTEX_BUFFER_BIT | VK_BUFFER_USAGE_STORAGE_BUFFER_BIT; break;
	case e_UBO:		mt = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT; break;
	case e_TBOs:	mt = VK_BUFFER_USAGE_TRANSFER_SRC_BIT; break;
	case e_INDEX:	mt = VK_BUFFER_USAGE_INDEX_BUFFER_BIT | VK_BUFFER_USAGE_STORAGE_BUFFER_BIT; break;
	case e_SSBO:	mt = VK_BUFFER_USAGE_STORAGE_BUFFER_BIT; break;
	case e_SSBOd:	mt = VK_BUFFER_USAGE_STORAGE_BUFFER_BIT; break;
	case e_INDIRECT:	mt	=	VK_BUFFER_USAGE_INDIRECT_BUFFER_BIT; break;

	case e_光追实例:	mt	=	VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT | 
								VK_BUFFER_USAGE_ACCELERATION_STRUCTURE_BUILD_INPUT_READ_ONLY_BIT_KHR ; 
						break;
	
	case e_光追顶层加速结构:
	case e_光追底层加速结构: mt =	VK_BUFFER_USAGE_ACCELERATION_STRUCTURE_STORAGE_BIT_KHR | 
									VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT; break;
	case e_着色列表:		mt =	VK_BUFFER_USAGE_TRANSFER_SRC_BIT | 
									VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT|
									VK_BUFFER_USAGE_SHADER_BINDING_TABLE_BIT_KHR ; 
		break;

	default:
		break;
	}

	return mt;
}

uint32 f_getAPI光追加速缓存类型(E_板载缓存类型 memType) {
	uint32 mt = 0;
	
	switch (memType) {
	case e_光追实例:	mt = VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT       |
		VK_BUFFER_USAGE_ACCELERATION_STRUCTURE_BUILD_INPUT_READ_ONLY_BIT_KHR |
		VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT                                  |
		VK_MEMORY_PROPERTY_HOST_COHERENT_BIT;
		break;

	case e_光追顶层加速结构: mt = VK_ACCELERATION_STRUCTURE_TYPE_TOP_LEVEL_KHR; break;
	case e_光追底层加速结构: mt = VK_ACCELERATION_STRUCTURE_TYPE_BOTTOM_LEVEL_KHR; break;

	default:
		break;
	}
	return mt;
}





S_VkArray缓存::S_VkArray缓存(S_设备环境& ctx, uint16 offset, E_板载缓存类型 type, uint64 size) {
	m_Data = { 0,0,0, {0} };
	m_Ctx = ctx;

	m_Size = size;
	m_Offset = offset;
	m_Type = type;
	m_VkBufferUsageFlags = f_getAPI缓存类型(type);
	m_光追加速结构 = nullptr;

	

	if (type == E_板载缓存类型::e_UBO) {
		m_Size = 1;
	}
	if (m_Size) {
		f_分配(m_VkBufferUsageFlags, offset * m_Size);
	}
	
}

S_VkArray缓存::S_VkArray缓存(S_设备环境& ctx, E_板载缓存类型 type, VkBufferUsageFlags typeflags, uint16 offset, uint64 size) {
	m_Data = { 0,0,0, {0} };
	m_Ctx = ctx;

	m_Size = size;
	m_Offset = offset;
	m_Type = type;
	m_VkBufferUsageFlags = typeflags;
	m_光追加速结构 = nullptr;

	if (type == E_板载缓存类型::e_UBO && !m_Size) {
		m_Size = 1;
	}
	if (m_Size) {
		f_分配(typeflags, offset * m_Size);
	}
}

S_VkArray缓存::~S_VkArray缓存() {
	if (m_光追加速结构) {
		auto di = f_vk_get绘图环境(m_Ctx);
		di->vkDestroyAccelerationStructure(m_Data.m_设备, m_光追加速结构, nullptr);
		m_光追加速结构 = nullptr;
	}

	if(m_Data.m_Mem) vkFreeMemory(m_Data.m_设备, m_Data.m_Mem, 0);
	if (m_Data.m_Buf) vkDestroyBuffer(m_Data.m_设备, m_Data.m_Buf, 0);
}



uint32 S_VkArray缓存::f_分配(VkBufferUsageFlags type, uint64 BitSize) {

	//static std::mutex g分配内存锁;
	m_分配内存锁.lock();
	//while (!m_分配内存锁) {
	//	std::cout<<"m_分配内存等待 锁"<<std::endl;
	//}
	//m_分配内存锁 = 0;
	//S_物理设备属性* 物理设备属性 = (S_物理设备属性*)m_Ctx.m_物理设备.pNext;
	//VkPhysicalDevice 物理设备 = (VkPhysicalDevice)(m_Ctx.m_物理设备.设备);
	S_物理设备属性* pp = f_vk_get物理设备属性(m_Ctx);
	m_Data.m_设备 = (VkDevice)(m_Ctx.m_逻辑设备.设备);
	auto di = f_vk_get绘图环境(m_Ctx);


	if (m_Data.m_Buf) {
		vkFreeMemory(m_Data.m_设备, m_Data.m_Mem, 0);
		vkDestroyBuffer(m_Data.m_设备, m_Data.m_Buf, 0);
	}


	VkBufferCreateInfo buf_info = {};
	buf_info.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
	buf_info.pNext = NULL;
	//buf_info.usage = type | VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT;
	buf_info.usage = type;
	buf_info.size = BitSize;
	buf_info.queueFamilyIndexCount = 0;
	buf_info.pQueueFamilyIndices = NULL;
	buf_info.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
	buf_info.flags = 0;


	if (pp->m_支持光追) {
		switch (m_Type) {
		case E_板载缓存类型::e_光追底层加速结构:
		case E_板载缓存类型::e_光追顶层加速结构: {
			buf_info.usage |= VK_BUFFER_USAGE_ACCELERATION_STRUCTURE_BUILD_INPUT_READ_ONLY_BIT_KHR;
			break;
		}
		default:
			break;
		}
	}
	

	VkResult res = vkCreateBuffer(m_Data.m_设备, &buf_info, NULL, &m_Data.m_Buf);
	assert(res == VK_SUCCESS);


	VkMemoryRequirements 内存请求信息;
	vkGetBufferMemoryRequirements(m_Data.m_设备, m_Data.m_Buf, &内存请求信息);
	assert(BitSize <= 内存请求信息.size);

	VkMemoryAllocateInfo 内存分配信息;
	{
		内存分配信息.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
		内存分配信息.pNext = NULL;
		内存分配信息.allocationSize = 内存请求信息.size;
		//内存分配信息.allocationSize = BitSize;

		switch (m_Type) {
		case E_板载缓存类型::e_光追底层加速结构:
		case E_板载缓存类型::e_光追顶层加速结构: {
			//内存分配信息.memoryTypeIndex = f_内存类型索引(pp->m_内存属性, 内存请求信息.memoryTypeBits, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT | VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
			内存分配信息.memoryTypeIndex = f_内存类型索引(pp->m_内存属性, 内存请求信息.memoryTypeBits, VK_MEMORY_PROPERTY_HOST_COHERENT_BIT | VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
			break;
		}
		default:
			内存分配信息.memoryTypeIndex = f_内存类型索引(pp->m_内存属性, 内存请求信息.memoryTypeBits,
				//VK_MEMORY_PROPERTY_HOST_COHERENT_BIT | VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT
				VK_MEMORY_PROPERTY_HOST_COHERENT_BIT | VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT
				//VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT
			);

			//没成功就切换大缓存
			/*uint32 heapIndex = di->m_内存属性.memoryTypes[内存分配信息.memoryTypeIndex].heapIndex;
			if (di->m_内存属性.memoryHeaps[heapIndex].size < 内存分配信息.allocationSize) {
				内存分配信息.memoryTypeIndex = f_内存类型索引(
					di->m_内存属性, 内存请求信息.memoryTypeBits, 
					VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT | VK_MEMORY_PROPERTY_HOST_CACHED_BIT
				);
			}*/
			break;
		}
	}

	VkMemoryAllocateFlagsInfoKHR allocFlagsInfo{};
	allocFlagsInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_FLAGS_INFO_KHR;
	allocFlagsInfo.flags = VK_MEMORY_ALLOCATE_DEVICE_ADDRESS_BIT_KHR;
	if (buf_info.usage & VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT) {
		内存分配信息.pNext = &allocFlagsInfo;
	}
	
	for (uint8 i = 0; i < 10; ++i) {
		res = vkAllocateMemory(m_Data.m_设备, &内存分配信息, NULL, &(m_Data.m_Mem));
		if (res != VK_SUCCESS) {
			//vkDeviceWaitIdle(m_Data.m_设备);
			Sleep(10);
		}
		else {
			break;
		}
	}
	
	if (res != VK_SUCCESS) {
		m_分配内存锁.unlock();
		return -1;
	}
	
	
	m_Data.m_Buffer_info.buffer = m_Data.m_Buf;
	m_Data.m_Buffer_info.offset = 0;
	m_Data.m_Buffer_info.range = BitSize;
	vkBindBufferMemory(m_Data.m_设备, m_Data.m_Buf, m_Data.m_Mem, 0);

	elt_capacity = BitSize;

	
	switch (m_Type) {
	case E_板载缓存类型::e_光追底层加速结构: 
	case E_板载缓存类型::e_光追顶层加速结构: {
		// Acceleration structure
		VkAccelerationStructureCreateInfoKHR 加速结构创建信息{};
		加速结构创建信息.sType = VK_STRUCTURE_TYPE_ACCELERATION_STRUCTURE_CREATE_INFO_KHR;
		加速结构创建信息.buffer = m_Data.m_Buf;
		加速结构创建信息.type = VkAccelerationStructureTypeKHR(f_getAPI光追加速缓存类型(m_Type));
		加速结构创建信息.size = BitSize;
		
		di->vkCreateAccelerationStructure(m_Data.m_设备, &加速结构创建信息, nullptr, &m_光追加速结构);
		break;
	}
	default:
		break;
	}

	//m_分配内存锁 = 1;
	m_分配内存锁.unlock();
	return 0;
}

void S_VkArray缓存::f_fillData(const void* data, uint64 bitSize) {
	if (m_Data.m_Buffer_info.range != bitSize) {
		//f_分配(f_getAPI缓存类型(type), bitSize);
		bitSize = ((bitSize>>1)<<1);
		f_分配(m_VkBufferUsageFlags, bitSize);
	}

	if (data && m_Data.m_Mem) {
		void* pData = 0;
		vkMapMemory(m_Data.m_设备, m_Data.m_Mem, 0, m_Data.m_Buffer_info.range, 0, &pData);
		memcpy(pData, data, m_Data.m_Buffer_info.range);
		vkUnmapMemory(m_Data.m_设备, m_Data.m_Mem);
		pData = 0;
	}
	
}

void S_VkArray缓存::f_map() {
	m_mappedPtr = 0;
	vkMapMemory(m_Data.m_设备, m_Data.m_Mem, 0, m_Data.m_Buffer_info.range, 0, &m_mappedPtr);

}

void S_VkArray缓存::f_unmap() {
	vkUnmapMemory(m_Data.m_设备, m_Data.m_Mem);
	m_mappedPtr = nullptr;


	/*VkMappedMemoryRange range{};
	range.sType = VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE;
	range.memory = m_Data.m_Mem;
	range.offset = 0;
	range.size = m_Data.m_Buffer_info.range;
	vkFlushMappedMemoryRanges(m_Data.m_设备, 1, &range);*/

}

void f_buf_刷新GPU内存(S_板载缓存* buf) {
	return;
	S_VkArray缓存* gpuBuf = (S_VkArray缓存*)buf;
	VkMappedMemoryRange range{};
	range.sType = VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE;
	range.memory = gpuBuf->m_Data.m_Mem;
	range.offset = 0;
	range.size = gpuBuf->m_Data.m_Buffer_info.range;
	vkFlushMappedMemoryRanges(gpuBuf->m_Data.m_设备, 1, &range);
}


void S_VkArray缓存::f_resize(uint64 bitSize) {
	assert(bitSize != 0);

	if (bitSize > m_Data.m_Buffer_info.range) {
		bitSize *= 1.33;
	}
	else if (bitSize < m_Data.m_Buffer_info.range * 0.77) {
		bitSize = m_Data.m_Buffer_info.range * 0.77;
	}
	else {
		return;
	}


	S_VkBuffer tData = m_Data;

	m_Data = { 0 };
	f_分配(m_VkBufferUsageFlags, bitSize);


	//m_分配内存锁.lock();
	f_copy((S_结构对象指针)&tData);
	//m_分配内存锁.unlock();

	if (tData.m_Buf) {
		vkFreeMemory(tData.m_设备, tData.m_Mem, 0);
		vkDestroyBuffer(tData.m_设备, tData.m_Buf, 0);
	}

	/*VkMappedMemoryRange range{};
	range.sType = VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE;
	range.memory = m_Data.m_Mem;
	range.offset = 0;
	range.size = m_Data.m_Buffer_info.range;
	vkFlushMappedMemoryRanges(m_Data.m_设备, 1, &range);*/
}

void S_VkArray缓存::f_copy(S_结构对象指针 sObj) {
	S_VkBuffer& tData = *((S_VkBuffer*)sObj);

	//判断是否为已有对象拷贝
	if (tData.m_Buf) {
		tData.m_Buffer_info = ((S_VkBuffer*)sObj)->m_Buffer_info;

		void* t_data = 0;

		vkMapMemory(m_Data.m_设备, m_Data.m_Mem, 0, m_Data.m_Buffer_info.range, 0, &m_mappedPtr);
		vkMapMemory(tData.m_设备, tData.m_Mem, 0, tData.m_Buffer_info.range, 0, &t_data);

		uint64 size = DEF_Min(m_Data.m_Buffer_info.range, tData.m_Buffer_info.range);
		memcpy(m_mappedPtr, t_data, size);

		vkUnmapMemory(m_Data.m_设备, m_Data.m_Mem);
		vkUnmapMemory(tData.m_设备, tData.m_Mem);


		//VkMappedMemoryRange range{};
		//range.sType = VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE;
		//range.memory = m_Data.m_Mem;
		//range.offset = 0;
		//range.size = m_Data.m_Buffer_info.range;
		//vkFlushMappedMemoryRanges(m_Data.m_设备, 1, &range);
	}
}

void S_VkArray缓存::f_change板载缓存(E_板载缓存类型 memType) {
	void* t_data = calloc(m_Size, m_Offset);

	m_mappedPtr = 0;
	vkMapMemory(m_Data.m_设备, m_Data.m_Mem, 0, m_Data.m_Buffer_info.range, 0, (void**)&m_mappedPtr);
	uint64 size = m_Size * m_Offset;
	memcpy(t_data, m_mappedPtr, size);
	vkUnmapMemory(m_Data.m_设备, m_Data.m_Mem);
	m_mappedPtr = 0;

	if (m_Data.m_Buf) {
		vkDestroyBuffer(m_Data.m_设备, m_Data.m_Buf, 0);
		vkFreeMemory(m_Data.m_设备, m_Data.m_Mem, 0);

		m_Data.m_Buf = 0;
		m_Data.m_Mem = 0;
	}

	f_分配(f_getAPI缓存类型(memType), m_Data.m_Buffer_info.range);

	f_map();
	memcpy(m_mappedPtr, t_data, size);
	f_unmap();

	free(t_data);
}




uint64 f_buf_getAddress(S_板载缓存* buf) {
	S_VkArray缓存* vkbuf = static_cast<S_VkArray缓存*>(buf);

	switch (buf->m_Type) {
	case E_板载缓存类型::e_光追底层加速结构:
	case E_板载缓存类型::e_光追顶层加速结构: {
		auto di = f_vk_get绘图环境(vkbuf->m_Ctx);
		VkAccelerationStructureDeviceAddressInfoKHR accelerationDeviceAddressInfo{};
		accelerationDeviceAddressInfo.sType = VK_STRUCTURE_TYPE_ACCELERATION_STRUCTURE_DEVICE_ADDRESS_INFO_KHR;
		accelerationDeviceAddressInfo.accelerationStructure = vkbuf->m_光追加速结构;
		return di->vkGetAccelerationStructureDeviceAddress(vkbuf->m_Data.m_设备, &accelerationDeviceAddressInfo);
	}
	default:
		VkBufferDeviceAddressInfo bufDeviceAI{};
		bufDeviceAI.buffer = vkbuf->m_Data.m_Buf;
		bufDeviceAI.sType = VK_STRUCTURE_TYPE_BUFFER_DEVICE_ADDRESS_INFO;
		return vkGetBufferDeviceAddress(vkbuf->m_Data.m_设备, &bufDeviceAI);
		break;
	}

	return 0;
}

uint64 f_buf_getAddress(S_GPU内存块索引& 块) {
	S_VkArray缓存* vkbuf = static_cast<S_VkArray缓存*>(块.m_Buf);

	VkDeviceAddress ptr;
	switch (vkbuf->m_Type) {
	case E_板载缓存类型::e_光追底层加速结构:
	case E_板载缓存类型::e_光追顶层加速结构: {
		auto di = f_vk_get绘图环境(vkbuf->m_Ctx);
		VkAccelerationStructureDeviceAddressInfoKHR accelerationDeviceAddressInfo{};
		accelerationDeviceAddressInfo.sType = VK_STRUCTURE_TYPE_ACCELERATION_STRUCTURE_DEVICE_ADDRESS_INFO_KHR;
		accelerationDeviceAddressInfo.accelerationStructure = vkbuf->m_光追加速结构;
		//accelerationDeviceAddressInfo.accelerationStructure = f_buf_get加速结构(块);
		ptr = di->vkGetAccelerationStructureDeviceAddress(vkbuf->m_Data.m_设备, &accelerationDeviceAddressInfo);
		break;
	}
	default:
		VkBufferDeviceAddressInfo bufDeviceAI{};
		bufDeviceAI.buffer = vkbuf->m_Data.m_Buf;
		bufDeviceAI.sType = VK_STRUCTURE_TYPE_BUFFER_DEVICE_ADDRESS_INFO;
		ptr = vkGetBufferDeviceAddress(vkbuf->m_Data.m_设备, &bufDeviceAI);
		break;
	}

	return ptr + uint64(块.m_Mem.m_偏移) * vkbuf->m_Offset;
	//return ptr + 块.m_Mem.m_偏移*vkbuf->m_Offset;
}



Inline uint64 f_GPU内存偏移ID(S_GPU偏移大小& 块) {
	return 块.m_偏移 | (uint64(块.m_大小)<<32);
}


S_GPU偏移大小 f_buf_galloc(S_板载缓存* buf, uint32 num) {
	S_GPU偏移大小 新块{};
	
	buf->m_分配块内存锁.lock();

	uint32 回收站元素数量 = buf->m_分配器.m_内存回收偏移.size();
	if (回收站元素数量) {
		auto* 回收ptr = buf->m_分配器.m_内存回收偏移.data();
		
		//uvec2 空闲内存;
		for (uint32 i = 0; i < 回收站元素数量; ++i) {
			S_GPU偏移大小 空闲内存 = 回收ptr[i];

			if (num <= 空闲内存.m_大小) {
				新块 = { 空闲内存.m_偏移, num};

				空闲内存.m_大小 -= num;
				if (空闲内存.m_大小) {
					空闲内存.m_偏移 += num;
					//cout<<"空闲内存.m_偏移 = "<<空闲内存.m_偏移<<" " << 空闲内存.m_大小<< " " << num << " " << i << endl;
					回收ptr[i] = 空闲内存;
				}
				else {
					buf->m_分配器.m_内存回收偏移.erase(buf->m_分配器.m_内存回收偏移.begin() + i);
				}

				break;
			}
		}
	}

	if(!新块.m_大小) {
		新块 = { buf->m_分配器.maxNum, num };
		//cout<<"新分配 = "<<buf->m_分配器.maxNum<<" " << 新块.m_大小 << endl;
		buf->m_分配器.maxNum += 新块.m_大小;
	}

	buf->m_分配器.m_内存分配偏移.insert(f_GPU内存偏移ID(新块));


	if (num) {
		if(buf->m_mappedPtr) buf->f_unmap();
		f_buf_resize(buf, buf->m_分配器.maxNum);
		buf->f_map();
	}

	buf->m_分配块内存锁.unlock();
	return 新块;
}

void f_buf_gfree(S_板载缓存* buf, S_GPU偏移大小& 块) {
	buf->m_分配块内存锁.lock();

	if (buf->m_分配器.m_内存分配偏移.find(f_GPU内存偏移ID(块)) != buf->m_分配器.m_内存分配偏移.end()) {
		buf->m_分配器.m_内存分配偏移.erase(f_GPU内存偏移ID(块));

		uint32 回收数量 = buf->m_分配器.m_内存回收偏移.size();
		auto* 回收ptr = buf->m_分配器.m_内存回收偏移.data();

		for (uint32 i = 0; i < 回收数量; ++i) {
			auto 回收块 = 回收ptr[i];
			if (回收块.m_偏移 == 块.m_偏移) {
				buf->m_分配器.m_内存回收偏移.erase(buf->m_分配器.m_内存回收偏移.begin() + i);
				buf->m_分配块内存锁.unlock();
				return;
			}
		}
		buf->m_分配器.m_内存回收偏移.push_back(块);
	}
	else {
		std::cout<<"释放GPU内存错误: 没有找到已有的块"<<std::endl;
	}
	buf->m_分配块内存锁.unlock();
}

void f_buf_gresize(S_GPU内存块索引& 块, uint32 newSize) {
	if (newSize > 块.m_Mem.m_大小 || newSize < 块.m_Mem.m_大小) {
		S_GPU内存块索引 新块 = f_buf_alloc(块.m_Buf, newSize);

		uint8* Dst = &(((uint8*)(新块.m_Buf->m_mappedPtr))[新块.m_Mem.m_偏移 * 新块.m_Buf->m_Offset]);
		uint8* Src = &(((uint8*)(块.m_Buf->m_mappedPtr))[块.m_Mem.m_偏移 * 块.m_Buf->m_Offset]);
		memcpy(Dst, Src, DEF_Min(块.m_Mem.m_大小, 新块.m_Mem.m_大小) * 块.m_Buf->m_Offset);

		if (新块.m_Mem.m_偏移 > 块.m_Mem.m_偏移 && 新块.m_Mem.m_偏移 < 块.m_Mem.m_偏移 + 块.m_Mem.m_大小) {
			std::cout<<新块.m_Mem.m_偏移<<std::endl;
		}
		if (块.m_Mem.m_偏移 > 新块.m_Mem.m_偏移 && 块.m_Mem.m_偏移 < 新块.m_Mem.m_偏移 + 新块.m_Mem.m_大小) {
			std::cout<<块.m_Mem.m_偏移<<std::endl;
		}
		if (块.m_Mem.m_偏移 < 新块.m_Mem.m_偏移 && 块.m_Mem.m_偏移 + 块.m_Mem.m_大小 > 新块.m_Mem.m_偏移) {
			std::cout<<新块.m_Mem.m_偏移<<std::endl;
		}
		if (新块.m_Mem.m_偏移 < 块.m_Mem.m_偏移 && 新块.m_Mem.m_偏移 + 新块.m_Mem.m_大小 > 块.m_Mem.m_偏移) {
			std::cout<<新块.m_Mem.m_偏移<<std::endl;
		}

		f_buf_erase(块);
		块 = 新块;
	}
}



//VkAccelerationStructureKHR f_vkBuf_create加速结构(S_VkArray缓存* buf, VkAccelerationStructureTypeKHR type, VkAccelerationStructureBuildSizesInfoKHR buildSizeInfo) {
	//VkAccelerationStructureKHR as{};
	// Acceleration structure
	/*VkAccelerationStructureCreateInfoKHR accelerationStructureCreate_info{};
	accelerationStructureCreate_info.sType = VK_STRUCTURE_TYPE_ACCELERATION_STRUCTURE_CREATE_INFO_KHR;
	accelerationStructureCreate_info.buffer = buf->m_Data.m_Buf;
	accelerationStructureCreate_info.size = buildSizeInfo.accelerationStructureSize;
	accelerationStructureCreate_info.type = type;
	vkCreateAccelerationStructureKHR(buf->m_Data.m_设备, &accelerationStructureCreate_info, nullptr, &as);

	// AS device address
	VkAccelerationStructureDeviceAddressInfoKHR accelerationDeviceAddressInfo{};
	accelerationDeviceAddressInfo.sType = VK_STRUCTURE_TYPE_ACCELERATION_STRUCTURE_DEVICE_ADDRESS_INFO_KHR;
	accelerationDeviceAddressInfo.accelerationStructure = as;
	vkGetAccelerationStructureDeviceAddressKHR(buf->m_Data.m_设备, &accelerationDeviceAddressInfo);*/
//	return as;
//}

void* f_buf_get加速结构(S_GPU内存块索引& 块) {
	auto* 绘图配置 = (S_绘图设备配置*)(块.m_Buf->m_Ctx.m_绘图设备配置);
	VkWriteDescriptorSetAccelerationStructureKHR* 光追结构传入参数 = ((VkWriteDescriptorSetAccelerationStructureKHR*)(块.m_Acc));
	
	
	if (光追结构传入参数) {
		绘图配置->vkDestroyAccelerationStructure(绘图配置->m_设备, (VkAccelerationStructureKHR)块.m_As, nullptr);
	}
	else {
		光追结构传入参数 = (VkWriteDescriptorSetAccelerationStructureKHR*)malloc(sizeof(VkWriteDescriptorSetAccelerationStructureKHR));

		(*光追结构传入参数) = { VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET_ACCELERATION_STRUCTURE_KHR };
		(*光追结构传入参数).accelerationStructureCount = 1;

		块.m_Acc = 光追结构传入参数;
	}


	VkAccelerationStructureCreateInfoKHR 加速结构创建信息{};
	加速结构创建信息.sType = VK_STRUCTURE_TYPE_ACCELERATION_STRUCTURE_CREATE_INFO_KHR;
	加速结构创建信息.buffer = ((S_VkArray缓存*)块.m_Buf)->m_Data.m_Buf;
	加速结构创建信息.type = VkAccelerationStructureTypeKHR(f_getAPI光追加速缓存类型(块.m_Buf->m_Type));
	加速结构创建信息.size = 块.m_Mem.m_大小;
	加速结构创建信息.offset = 块.m_Mem.m_偏移;

	绘图配置->vkCreateAccelerationStructure(绘图配置->m_设备, &加速结构创建信息, nullptr, (VkAccelerationStructureKHR*)&(块.m_As));
	//绘图配置->vkCreateAccelerationStructure(绘图配置->m_设备, &加速结构创建信息, nullptr, ((S_VkArray缓存*)块.m_Buf)->m_光追加速结构));

	光追结构传入参数->pAccelerationStructures = (VkAccelerationStructureKHR*)&(块.m_As);
	//光追结构传入参数->pAccelerationStructures = &((S_VkArray缓存*)块.m_Buf)->m_光追加速结构;

	//return ((S_VkArray缓存*)块.m_Buf)->m_光追加速结构;
	return 块.m_As;
}



